package com.lsy.weizhi2.popupwindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.customview.GlideCircleTransform;
import com.lsy.weizhi2.mybase.BaseApp;
import com.lsy.weizhi2.myutils.DaShangPopupCall;
import com.zhy.autolayout.utils.AutoUtils;

/**
 * Created by 嗨哟 on 2017/3/16.
 */
public class DaShangPopup implements View.OnClickListener{
    private TextView popup_cancle,popup_title,popup_tv_1,popup_tv_2,popup_phone,liuyan_popup;
    private ImageView popup_image;
    private Button popup_btn;
    private EditText popup_price;
    private PopupWindow dashangPopup;
    private DaShangPopupCall daShangPopupCall;
    private RelativeLayout popup_down;
    private int orderReplyId;
    private int flag;
    public void init(Context context,View parent,String title,String tv_1,String tv_2,int flag){
        this.flag=flag;
        View view=View.inflate(context, R.layout.dashangpopup,null);
        AutoUtils.autoSize(view);
        popup_cancle= (TextView) view.findViewById(R.id.popup_cancle);
        popup_image= (ImageView) view.findViewById(R.id.popup_image);
        popup_btn= (Button) view.findViewById(R.id.popup_btn);
        popup_title= (TextView) view.findViewById(R.id.popup_title);
        popup_price= (EditText) view.findViewById(R.id.popup_price);
        popup_tv_2= (TextView) view.findViewById(R.id.popup_tv_2);
        popup_tv_1= (TextView) view.findViewById(R.id.popup_tv_1);
        popup_title.setText(title);
        if(TextUtils.isEmpty(tv_1)){
            popup_tv_1.setVisibility(View.GONE);
        }else{
            popup_tv_1.setText(tv_1);
        }
        if(TextUtils.isEmpty(tv_2)){
            popup_tv_2.setVisibility(View.GONE);
        }else{
            popup_tv_2.setText(tv_2);
        }
        dashangPopup=new PopupWindow(context);
        dashangPopup.setContentView(view);
        dashangPopup.setWidth(BaseApp.SCREEN_WIDTH*460/750);
        dashangPopup.setHeight(BaseApp.REAL_HEIGHT*700/1334);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        dashangPopup.setBackgroundDrawable(dw);
        dashangPopup.setFocusable(true);
        dashangPopup.showAtLocation(parent, Gravity.CENTER,0,0);
        popup_cancle.setOnClickListener(this);
        popup_btn.setOnClickListener(this);
        dashangPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                daShangPopupCall.dismissListener();
            }
        });
    }

    public void yuyue(Context context,View parent,int avatar,int flag,int orderReplyId){
        this.flag=flag;
        this.orderReplyId=orderReplyId;
        View view=View.inflate(context, R.layout.dashangpopup,null);
        AutoUtils.autoSize(view);
        popup_title= (TextView) view.findViewById(R.id.popup_title);
        popup_tv_1= (TextView) view.findViewById(R.id.popup_tv_1);

        popup_cancle= (TextView) view.findViewById(R.id.popup_cancle);
        popup_image= (ImageView) view.findViewById(R.id.popup_image);
        popup_btn= (Button) view.findViewById(R.id.popup_btn);
        popup_price= (EditText) view.findViewById(R.id.popup_price);
        if(flag==DaShangPopupCall.JuBao){
            popup_title.setVisibility(View.GONE);
            popup_tv_1.setVisibility(View.GONE);
            popup_btn.setText("确认举报");
            popup_cancle.setText("不举报了");
        }
        Glide.with(context).load(new GlideUrl(Constants.DOWN_USER_PICTURE+avatar,new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID",MyInfo.User_Id+"").build()))
                .transform(new GlideCircleTransform(context)).error(R.mipmap.default_head).into(popup_image);
        dashangPopup=new PopupWindow(context);
        dashangPopup.setContentView(view);
        dashangPopup.setWidth(BaseApp.SCREEN_WIDTH*460/750);
        dashangPopup.setHeight(BaseApp.REAL_HEIGHT*700/1334);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        dashangPopup.setBackgroundDrawable(dw);
        dashangPopup.setFocusable(true);
        dashangPopup.showAtLocation(parent, Gravity.CENTER,0,0);
        popup_cancle.setOnClickListener(this);
        popup_btn.setOnClickListener(this);
        dashangPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                daShangPopupCall.dismissListener();
            }
        });

    }



    public void connect(Context context,View parent,int flag,String phone,String msg){
        this.flag=flag;
        View view = View.inflate(context,R.layout.connectpopup,null);
        AutoUtils.autoSize(view);
        popup_btn= (Button) view.findViewById(R.id.popup_btn);
        popup_title= (TextView) view.findViewById(R.id.popup_title);
        popup_phone= (TextView) view.findViewById(R.id.popup_phone);
        if(phone!=null){
            popup_phone.setText(phone);
        }else if(msg!=null){
            popup_phone.setText(msg);
            popup_btn.setText("私信联系");
        }
        dashangPopup=new PopupWindow(context);
        dashangPopup.setContentView(view);
        dashangPopup.setWidth(BaseApp.SCREEN_WIDTH*460/750);
        dashangPopup.setHeight(BaseApp.REAL_HEIGHT*500/1334);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        dashangPopup.setBackgroundDrawable(dw);
        dashangPopup.setFocusable(true);
        dashangPopup.showAtLocation(parent, Gravity.CENTER,0,0);
        popup_btn.setOnClickListener(this);
        dashangPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                daShangPopupCall.dismissListener();
            }
        });
    }

    public void invite(Context context,View parent,int flag,String title,String content,String tv_2,String btn){
        this.flag=flag;
        View view = View.inflate(context,R.layout.invitepopup,null);
        AutoUtils.autoSize(view);
        popup_image= (ImageView) view.findViewById(R.id.popup_image);
        popup_btn= (Button) view.findViewById(R.id.popup_btn);
        popup_btn.setText(btn);
        popup_title= (TextView) view.findViewById(R.id.popup_title);
        popup_title.setText(title);
        popup_price= (EditText) view.findViewById(R.id.popup_price);
        popup_tv_2= (TextView) view.findViewById(R.id.popup_tv_2);
        popup_tv_2.setText(tv_2);
        popup_cancle= (TextView) view.findViewById(R.id.popup_cancle);
        liuyan_popup= (TextView) view.findViewById(R.id.liuyan_popup);
        dashangPopup=new PopupWindow(context);
        dashangPopup.setContentView(view);
        dashangPopup.setWidth(BaseApp.SCREEN_WIDTH*460/750);
        dashangPopup.setHeight(BaseApp.REAL_HEIGHT*760/1334);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        dashangPopup.setBackgroundDrawable(dw);
        dashangPopup.setFocusable(true);
        dashangPopup.showAtLocation(parent, Gravity.CENTER,0,0);
        if(TextUtils.isEmpty(content)){
            liuyan_popup.setVisibility(View.GONE);
            dashangPopup.setHeight(BaseApp.REAL_HEIGHT*700/1334);
        }else{
            liuyan_popup.setText(content);
        }
        popup_cancle.setOnClickListener(this);
        popup_btn.setOnClickListener(this);
        dashangPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                daShangPopupCall.dismissListener();
            }
        });
    }

    public void setDaShangPopupCall(DaShangPopupCall daShangPopupCall){
        this.daShangPopupCall=daShangPopupCall;
    }

    public String getPrice(){
        return popup_price.getText().toString().trim();
    }

    public String getPhone(){
        return popup_phone.getText().toString();
    }

    public String getBtn(){return popup_btn.getText().toString();}
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.popup_btn:
                if(flag==DaShangPopupCall.YUYUE){
                    String str=popup_price.getText().toString().trim();
                    daShangPopupCall.yuyue(str);
                }else if(flag==DaShangPopupCall.JuBao){
                    String str=popup_price.getText().toString().trim();
                    daShangPopupCall.jubao(str,orderReplyId);
                }else{
                    daShangPopupCall.connect(view,flag);
                }
                break;
            case R.id.popup_cancle:
                dashangPopup.dismiss();
                break;
        }
    }
    public void dismiss(){
        dashangPopup.dismiss();
    }
}
